package ServletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * 获取context对象
 */
@WebServlet("/ServletContext_demo4")
public class ServletContext_demo4 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


        ServletContext context = this.getServletContext();
        //src目录下的文件
        String path1 = context.getRealPath("/WEB-INF/classes/a.txt");
        //web-inf
        String path2 = context.getRealPath("/WEB-INF/b.txt");
        //根目录下的
        String path3 = context.getRealPath("/c.txt");
        System.out.println(path1);
        System.out.println(path2);
        System.out.println(path3);
//      File file = new File(path);
    }
}
